package models

import (
	"go-project/common"
)

type Admin struct {
	ID          uint              `gorm:"primary_key" json:"id"`
	CreatedAt   *common.LocalTime `json:"created_at"`
	UpdatedAt   *common.LocalTime `json:"updated_at"`
	DeletedAt   *common.LocalTime `sql:"index" json:"deleted_at"`
	Password    string            `gorm:"type:varchar(100);not null;comment:'密码'" json:"password"`
	Username    string            `gorm:"not null;default:'';unique_index;comment:'用户名'" json:"username"`
	Email       string            `gorm:"type:varchar(100);not null;unique_index;default:'';comment:'邮箱'" json:"email"`
	Phone       string            `gorm:"type:varchar(20);not null;unique_index;default:'';comment:'手机号'" json:"phone"`
	LastLoginAt *common.LocalTime `gorm:"comment:'最后一次登录时间'" json:"last_login_at"`
	LoginIp     string            `gorm:"type:varchar(100);default:'';comment:'登录ip'" json:"login_ip"`
	Address     string            `gorm:"type:varchar(255);default:'';comment:'地址'" json:"address"`
	Desc        string            `gorm:"type:text;not null;comment:'描述'" json:"desc"`
	Province    string            `json:"province"`
	City        string            `json:"city"`
	County      string            `json:"county"`
	RoleId      uint              `json:"role_id"`
}

func (admin *Admin) TableName() string {
	return "admins"
}

func (admin *Admin) CheckUsernameExist(username string, id uint) bool {
	query := common.DB.Where("username=?", username)
	if id != 0 {
		query = query.Not("id", id)
	}
	if query.First(admin).RecordNotFound() {
		return false
	}
	return true
}

func (admin *Admin) CheckEmailExist(username string, id uint) bool {
	query := common.DB.Where("email=?", username)
	if id != 0 {
		query = query.Not("id", id)
	}
	if query.First(admin).RecordNotFound() {
		return false
	}
	return true
}

func (admin *Admin) CheckPhoneExist(phone string, id uint) bool {
	query := common.DB.Where("phone=?", phone)
	if id != 0 {
		query = query.Not("id", id)
	}
	if query.First(admin).RecordNotFound() {
		return false
	}
	return true
}
